package cliente;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author triki
 */
public class Servidor {

    public ServerSocket sock;

    public Servidor()
    {
        try {
            sock = new ServerSocket(9999);
        } catch (IOException ex) {
            Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args)
    {
       Servidor server = new Servidor();
       File f;
       byte[] aux = new byte[100];
       
       while(true)
       {
            try
            {
                f = new File("./Nightwish11.mp3");
                //File g = new File("./I_ICan.mp3");

                Socket s = server.sock.accept();
                System.out.println("Cliente aceptado...");
                OutputStream osock = s.getOutputStream();
                int j=0;
                FileInputStream ifile = new FileInputStream(f);
                //FileOutputStream gfile = new FileOutputStream(g);
                int i=0;
                while(ifile.read(aux) > -1)
                {
                   System.out.println("Enviado: "+i*100+"bytes");
                   osock.write(aux);
                   //gfile.write(aux);
                   i++;
                }
                osock.write(aux);
                //gfile.write(aux);
                s.close();
                System.out.println("Fichero terminado");

            } catch (IOException ex) {
                Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
            }
       }
    }
}
